import time

from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait

from config import PATH


class BasePage(object):
    # 获取浏览器对象
    def __init__(self, driver):
        self.driver = driver

    # 设置元素定位的方法
    def get_element(self, loc):
        try:
            """
         元素定位的公共方法
        :param loc:定位到元素的属性
        :return:定位到元素，返回值
        """
            element = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(*loc))
            return element
        except Exception as e:
            print("元素定位失败".format(e))
            raise

    def get_shot(self, file_name):
        """

        :param file_name:
        :return:无
        """
        self.driver.get_screenshot_as_file(PATH + '/img/' + file_name)

    #     清除和输入
    def base_clear_input(self, loc, text):
        """
        清除输入框
        :param loc: 定位到元素的属性
        :param text: 输入的内容
        :return: 无
        封装前：
        self.get_element(self.phone).clear
        self.get_element(self.phone).send_keys(phone)
        self.get_element(self.phone).clear
        self.get_element(self.password).send_keys(password)
        e
        """
        element = self.get_element(loc)
        element.clear()
        element.send_keys(text)

    #     点击元素
    def click_element(self, loc):
        """
        点击元素
        :param loc: 定位到元素的属性
        :return: 无
        lf.get_element(self.login).click()
        """
        self.get_element(loc).click()

    #     切换窗口
    def switch_window(self, loc):
        """

        :param loc:
        :return:
        """
        time.sleep(3)
        windows = self.driver.window_handles
        self.driver.switch_to.window(windows[1])
        element = self.get_element(loc)
        return element
    # 切换到frame'
    def switch_frame(self, loc):
        """
        :param loc:
        :return:
        """
        frame = self.get_element(loc)
        self.driver.switch_to.frame(frame)


    # 定义鼠标悬停
    def move_to_element(self, loc1):
        """
        鼠标悬停,定位元素
        :param loc1:
        :return:
        """
        element = self.get_element(loc1)
        ActionChains(self.driver).move_to_element(element).perform()

    def scroll(self,x,y):
        js = "window.scrollTo({},{})".format(x,y)
        self.driver.execute_script(js)





